Repository navigation
Reject invalid runId in JSON-RPC requests with InvalidParams error - #8665
Conversation
Fixes #8423. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a JSON-RPC protocol bug where invalid runId values in testing/discoverTests / testing/runTests requests were silently coerced to Guid.Empty. The deserializers now throw MessageFormatException for unparseable run IDs, and the RPC message deserializer wraps per-method param deserialization in a try/catch that captures failures in a new InvalidRequestParamsArgs sentinel so the request can still flow to the handler and produce a proper JSON-RPC InvalidParams (-32602) error response.
Changes:
- Both serializer paths (System.Text.Json
Json.csand JsoniteSerializerUtilities.cs) now reject invalidrunIdand route param-deserialization failures through a new sentinel type. ServerTestHosttranslates the sentinel into a newJsonRpcException, then into a properly coded JSON-RPC error response.- Tests added for direct deserializer behavior, full
RpcMessagesentinel capture, and an end-to-end server test; pre-existingDiscoveryRequestCanBeCanceledtest updated to use a valid GUID.
Show a summary per file
| File | Description |
|---|---|
| src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.cs | STJ path: throws MessageFormatException on invalid runId; wraps params parse in try/catch and reads id first to build sentinel for requests. |
| src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs | Jsonite path: mirrors the same invalid-runId throw and sentinel-on-failure behavior. |
| src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/RpcMessages.cs | Adds InvalidRequestParamsArgs sentinel record. |
| src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/JsonRpcException.cs | New internal exception carrying an ErrorCode to bridge handler to error response. |
| src/Platform/Microsoft.Testing.Platform/Hosts/ServerTestHost.cs | Handles sentinel by throwing JsonRpcException; sends InvalidParams error response and completes request state. |
| test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/FormatterUtilitiesTests.cs | Tests for per-args throw and full RpcMessage sentinel capture across both formatters. |
| test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/ServerTests.cs | Fixes prior invalid runId value and adds end-to-end test that the server returns InvalidParams and stays alive. |
Copilot's findings
- Files reviewed: 7/7 changed files
- Comments generated: 0
Amaury Levé (Evangelink)
left a comment
There was a problem hiding this comment.
Review Summary
The core design is sound: the sentinel + JsonRpcException plumbing correctly keeps the message loop alive and sends the right -32602 InvalidParams error code for the targeted runId validation case. The cleanup of the "Run1" test fixture is a necessary correctness fix.
However, the exception guard that was added to route deserialization failures through the sentinel path is incomplete: it only catches MessageFormatException, but two adjacent code paths throw different exception types that escape the guard:
SerializerUtilities.cs— wrong-typed fields ("runId": 42):GetRequiredPropertyFromJson<string>does an unsafe(T)unboxing cast →InvalidCastException, not caught → client receives error code0withe.ToString()(stack trace leak) instead of-32602.Json.cs— missing field (runIdabsent entirely):Bind<string>callselement.GetProperty(property)which throwsKeyNotFoundException, notMessageFormatException, same outcome.
Both scenarios involve untrusted client input arriving at the same trust boundary the PR is hardening, so they are in scope to fix here.
The two MAJOR inline comments include concrete reproduction payloads and suggested fixes. The remaining comments are a NIT (duplicate string) and a MODERATE (missing runTests integration test).
Generated by Expert Code Review (on open) for issue #8665 · sonnet46 6.6M
- Bind<T> (Json.cs) throws MessageFormatException on missing property instead of KeyNotFoundException. - GetRequiredPropertyFromJson<T> (SerializerUtilities.cs) throws MessageFormatException on wrong-typed property instead of InvalidCastException. - Broadened RpcMessage param-deserialization catch in both parsers to also handle InvalidOperationException/JsonException (Json.cs) and InvalidCastException (SerializerUtilities.cs), so any untrusted-input deserialization failure routes through the InvalidRequestParamsArgs sentinel and produces a JSON-RPC -32602 InvalidParams response. - Extracted shared InvalidRunIdErrorMessage constant on JsonRpcStrings. - TrySetException for JsonRpcException in ServerTestHost for defensive consistency. - Parameterized ServerTests integration test (DiscoverTests + RunTests) and added RpcMessage-level coverage for wrong-typed and missing runId. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Fixes #8423.
Problem
When a client sends a JSON-RPC request (
testing/discoverTestsortesting/runTests) with a malformedrunIdvalue (e.g., a non-GUID string), the deserializer silently coerced it toGuid.EmptyviaGuid.TryParse. This hid the protocol violation from the client and could cause hard-to-diagnose downstream behavior.Fix
DiscoverRequestArgsandRunRequestArgsdeserializers in bothJson.cs(System.Text.Json) andSerializerUtilities.cs(Jsonite) now throwMessageFormatExceptionwhenrunIdcannot be parsed as aGuid.RpcMessagedeserializer now parsesidfirst, then wraps the per-method params deserialization in atry/catch. On failure, it embeds anInvalidRequestParamsArgs(ErrorCode, ErrorMessage)sentinel inParamsso the message can flow through to the request handler without crashing the server's message-handling loop.ServerTestHost.HandleRequestCoreAsyncdetects the sentinel and throws a newJsonRpcExceptioncarrying the proper error code;HandleRequestAsynccatches it and sends a JSON-RPCInvalidParams(-32602) error response to the client.Tests
FormatterUtilitiesTestscoverage for invalidrunIdon both args types (per-args deserializer throws; fullRpcMessagedeserializer captures sentinel).ServerTestsintegration test that sends a malformed request, asserts anInvalidParamserror is returned, and verifies the server cleanly handles the subsequentexitnotification (i.e., the loop is still alive).DiscoveryRequestCanBeCanceledtest that used an invalidrunIdofRun1(now uses a valid GUID).